package dk.hewison.client.album;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import dk.hewison.client.AppEvent;
import dk.hewison.client.domain.Directory;
import dk.hewison.client.mvc.Register;
import dk.hewison.client.panels.CompositeView;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-15 12:47:07 +0000 (Sun, 15 Feb 2009) $:
 * @version $Revision: 364 $:
 */
public class DirectoryPathView extends CompositeView<Directory> {
    private FlowPanel flowPanel;

    private Button slideShowButton;

    public void initView() {
        flowPanel = new FlowPanel();

        slideShowButton = new Button("->SlideShow");
        slideShowButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {

                Register.getInstance().eventOccurred(new AppEvent<Directory>(AppEvent.Event.START_SLIDESHOW, getModel()));
            }
        });

        initWidget(flowPanel);

        setStyleName("directorypath");
    }


    public void setModel(Directory directory) {
        super.setModel(directory);
        flowPanel.clear();
        flowPanel.add(makeLink(directory, false));

        Directory parentDir = directory;
        do {
            parentDir = parentDir.getParentDirectory();
            if (parentDir != null) {
                flowPanel.insert(makeLink(parentDir, true), 0);
            }
        } while (parentDir != null);
        flowPanel.add(slideShowButton);
    }

    private Widget makeLink(final Directory directory, boolean clickable) {
        Button b = new Button(directory.getFolderName());
        if (clickable) {
            b.addClickListener(new ClickListener() {
                public void onClick(Widget widget) {
                    Register.getInstance().eventOccurred(new AppEvent<Directory>(AppEvent.Event.ALBUM_SELECTED, directory));
                }
            });
        }
        return b;
    }
}
